package Born.slider
{
	import com.greensock.TweenLite;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.utils.getDefinitionByName;
	
	import novesfora.events.Events;
	import novesfora.loader.LoadFile;

	public class ChangeObjects
	{
		private var $id						:uint; // Responsavel por marcar o id da imagem a ser mostrada
		private var $idMax					:uint; // Responsavel por marcar a quantidade maxima que o $id por ir
		private var $load					:LoadFile; // Loader dos objetos a serem carregados
		private var $content				:MovieClip; // Movieclip que vai receber os objetos
		private var $mask					:MovieClip; // Movieclip que faz mascara no Conteudo
		private var $box					:MovieClip // Movieclip dentro de $content
		private var $objects				:Array; // Array com todos os objetos a serem carregados
		private var $loader					:String; // String que traz o nome da classe de loader
		private var $loaderClip				:MovieClip; // Movieclip que ira ser criado com o $loader
		private var $time					:Number; // Tempo de transicao das animacoes
		
		public function ChangeObjects($conteudo:MovieClip,$mascara:MovieClip,$right:MovieClip,$left:MovieClip,$imagens:Array,$carregador:String = null)
		{
			$conteudo.mask = $mascara;
			$idMax = $imagens.length - 1;
			$content = $conteudo;
			$mask = $mascara;
			$objects = $imagens;
			$loader = $carregador;
			$time = 1;
			
			$box = new MovieClip();
			$content.addChild($box);
			
			$right.buttonMode = true;
			$right.addEventListener(MouseEvent.CLICK, CallNextImage);
			
			$left.buttonMode = true;
			$left.addEventListener(MouseEvent.CLICK, CallPrevImage);
			
			ShowPicture();
		}
		
		private function CallNextImage(e:MouseEvent = null):void
		{			
			if ( $id < $idMax )
			{
				$id++;
				ShowPicture();
			}
		}
		
		private function CallPrevImage(e:MouseEvent = null):void
		{
			if ( $id > 0 )
			{
				$id--;
				ShowPicture();
			}
		}
		
		private function ShowPicture():void
		{
			TweenLite.to($box, $time * 0.5, { 	alpha : 0,
												onComplete : function():void
												{
													try
													{
														$load.removeEventListener(Events.COMPLETE, LoadComplete);
														$load.removeEventListener(Events.IOError, LoadError);
														$load.removeEventListener(Events.PROGRESS, LoadProgress);
														
														$content.removeChild($loaderClip);
													}
													catch(e:*) {}
													while ( $box.numChildren != 0 ) $box.removeChildAt(0);
													$load = new LoadFile($objects[$id]);
													$load.addEventListener(Events.COMPLETE, LoadComplete);
													$load.addEventListener(Events.IOError, LoadError);
													$load.addEventListener(Events.PROGRESS, LoadProgress);
													
													if ( $loader != null )
													{
														$loaderClip = new (getDefinitionByName($loader) as Class)();
														$loaderClip.x = ($mask.width - $loaderClip.width) * 0.5;
														$loaderClip.y = ($mask.height - $loaderClip.height) * 0.5;
														
														$content.addChild($loaderClip);
													}
												}
			}); // End of Tweenlite			
		}
				
		private function LoadComplete(e:Events):void
		{
			if ( $loader != null )
			{
				$content.removeChild($loaderClip);
			}
			
			$box.alpha = 0;
			$box.addChild($load);
			TweenLite.to($box, $time, { alpha : 1 });
		}
		
		private function LoadProgress(e:Events):void
		{
			trace(e.obj.PercentLoaded);
		}
		
		private function LoadError(e:Events):void
		{
			trace("Born > ChangeObjects: Error on Load object");
		}
		
		public static function add(conteudo:MovieClip,mascara:MovieClip,right:MovieClip,left:MovieClip,imagens:Array,carregador:String = null):ChangeObjects
		{
			return new ChangeObjects(conteudo,mascara,right,left,imagens,carregador);
		}
	}
}